﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Masood.MobileGames.SlidingLib.Enum;
using Microsoft.Phone.Tasks;
using Microsoft.Phone;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.IO;

namespace Masood.MobileGames.Sliding
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }       
        
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //easy number puzzle game clicked.
            App.GameLevel = GameEnum.Easy;
            NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //medium number puzzle game clicked.
            App.GameLevel = GameEnum.Medium;
            NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //hard number puzzle game clicked.
            App.GameLevel = GameEnum.Hard;
            NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            App.WriteableBitmap = null;
            
            //apple game clicked.
            //ChoosePhoto(sender, e);

            //App.GameLevel = GameEnum.ForKidApple;
            //NavigationService.Navigate(new Uri("/ImageGampePage.xaml", UriKind.Relative));

            App.GameLevel = GameEnum.Custom;
            if (App.WriteableBitmap != null)
                App.GameLevel = GameEnum.Picture;
            
            NavigationService.Navigate(new Uri("/Custom.xaml", UriKind.Relative));
        }

        private void ChoosePhoto(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask taskToChoosePhoto = new PhotoChooserTask();
            taskToChoosePhoto.PixelHeight = (int) this.ActualHeight;
            taskToChoosePhoto.PixelWidth = (int) this.ActualWidth;
            taskToChoosePhoto.Show();
            taskToChoosePhoto.Completed += new EventHandler<PhotoResult>(taskToChoosePhoto_Completed);
        }

        void taskToChoosePhoto_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                string fileName = e.OriginalFileName;
                App.SelectedFileName = e.OriginalFileName;

                Uri uri = new Uri(fileName, UriKind.Relative); 
                StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
                
                BitmapImage image = new BitmapImage();
                image.SetSource(resourceInfo.Stream);
                App.SelectedBitmap = image;
                App.SelectedImageStream = resourceInfo.Stream;

                Stream s = resourceInfo.Stream;
                long length = s.Length;


                WriteableBitmap selectedPhoto = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
                App.WriteableBitmap = selectedPhoto;
            }
        }
    }
}